package A2;

import java.util.ArrayList;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 * <p>Title: Class to populate some widgets into the db</p>
 * <p>Description: CS 343 Assignment #2</p>
 * @author Mike Harmon
 * @author Erik Olson
 * @author Casey Golla
 * @author Jack Freiermuth
 * @email michael.harmon@my.uwrf.edu
 * @email erik.olson@my.uwrf.edu
 * @email casey.golla@my.uwrf.edu
 * @email jack.freiermuth@my.uwrf.edu
 * @date November 4th 2013
 * @team 5
 */ 
public class loadDataBase {
	private static ArrayList<Widget> widgets;

	public static void main(String[] args) {
		// Create an inventory of Widgets
		widgets = new ArrayList<Widget>();
		widgets.add( new Widget( "Widget 1", "The first kind of widget" ) );
		widgets.add( new Widget( "Widget 2", "The second kind of widget" ) );
		widgets.add( new Widget( "Widget 3", "The third kind of widget" ) );
		widgets.add( new Widget( "Widget 4", "The fourth kind of widget" ) );
		widgets.add( new Widget( "Widget 5", "The fifth kind of widget" ) );
		widgets.add( new Widget( "Widget 6", "The sixth kind of widget" ) );
		widgets.add( new Widget( "Widget 7", "The seventh kind of widget" ) );
		widgets.add( new Widget( "Widget 8", "The eighth kind of widget" ) );
		widgets.add( new Widget( "Widget 9", "The nine kind of widget" ) );
		widgets.add( new Widget( "Widget a", "The a kind of widget" ) );
	    
	    final String PERSISTENCE_UNIT_NAME = "WidgetsAndOrders";
		EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
		
	    EntityManager em = factory.createEntityManager();

	    // create new widget to persist into the db
	    em.getTransaction().begin();
	    
	    // Persist all
	    for (Widget w : widgets)
	    	em.persist(w);
	    
	    em.getTransaction().commit();
	    em.close();
	    
	    System.out.println( "Widgets put into DB" );
	}

}
